//
//  statusModel.swift
//  weibo
//
//  Created by 陈伟 on 15/10/19.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

class StatusViewModel: CustomStringConvertible  {
    
    var status: Status
    
    init(status: Status){
        self.status = status
        
        if (status.retweeted_status?.pic_urls?.count > 0) {
            
            thumbnailUrls = [NSURL]()
            for dict in (status.retweeted_status?.pic_urls)! {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnailUrls?.append(url!)
            }
        }
            if status.pic_urls?.count > 0 {
//            初始化数组
            self.thumbnailUrls = [NSURL]()
            for dict in (status.pic_urls)! {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                self.thumbnailUrls?.append(url!)
        }
    }
}
    //表格的空重用标示符
    var cellId: String {
        return status.retweeted_status != nil ? RetweedStatusCellID : NormalStatusCellID
    }
    
    //缓存行高
    lazy var rowHeight: CGFloat = {
        
        var cell: StatusCell
        
        if self.status.retweeted_status != nil {
           cell = StatusRetweetedCell(style: .Default, reuseIdentifier: RetweedStatusCellID)
        }else {
            cell = StatusNormalCell(style: UITableViewCellStyle.Default, reuseIdentifier: NormalStatusCellID)
        }
        return cell.rowHeight(self)
    }()
    
    //用户头像
    var userProfileUrl: NSURL {
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    //默认头像
    var userDefaultIconView: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    
    //用户昵称
    var nameLabel: UILabel {
        return UILabel(title: (status.user?.screen_name)!, fontsize: 14)
    }
    
    //是否是VIp
    var userMemberImage: UIImage?{
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user?.mbrank)")
        }
        return nil
    }
    //是否认证
    var vipIconView: UIImage? {
        
        switch(status.user?.verified_type ?? -1){
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    
    //转发微博相关
    //存储型属性。存储配图地址
    var thumbnailUrls: [NSURL]?
    
    var retweetedText: String? {
        guard let s = status.retweeted_status else {
            return nil
            
        }
        return "@" + (s.user?.screen_name)! + ":" + (s.text ?? "")
    }
    
    var description: String {
        return status.description + (thumbnailUrls?.description)!
    }
    
}
